package org.thraex.excel.utils;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.log4j.Log4j2;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.thraex.excel.vo.DetailVO;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @Author 鬼王
 * @Date 2019/08/12 10:29
 */
@Log4j2
public class ExportExcelOrgUtil {

    public static XSSFWorkbook zxExprotExcelXLSX(List<DetailVO> detailVOS, String month) {
        XSSFWorkbook wb = new XSSFWorkbook();

        CellStyle titleCellStyle = getTitleStyle(wb);

        // 获取单位列表
        List<DetailVO> orgList = detailVOS.stream().distinct().filter(it -> it.getDeptName() != null).collect(Collectors.toList());
        orgList.forEach(it -> System.out.println("Detp Name: " + it.getDeptName()));

        ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat("excelOrg-pool-%d")
                .build();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 200, 0L,
                TimeUnit.MICROSECONDS,
                new LinkedBlockingDeque<>(1024), threadFactory,
                new ThreadPoolExecutor.AbortPolicy());
        CountDownLatch countDownLatch = new CountDownLatch(orgList.size());

        orgList.forEach(it -> {
            ExportExcelOrgThread thread = new ExportExcelOrgThread();
            thread.setTitleCellStyle(titleCellStyle);
            thread.setDetailVOS(detailVOS);
            thread.setWb(wb);
            thread.setMonth(month);
            thread.setCountDownLatch(countDownLatch);
            System.out.println("each: " + it.getDeptName());
            thread.setDetailVO1(it);
            threadPoolExecutor.submit(thread);
//            log.info(thread.getName() + "---Start");
        });

        log.info("777777");
        try {
            log.info("8888");
            countDownLatch.await();
            log.info("999");
        } catch (InterruptedException e) {
            log.error("线程计数异常" + e.getMessage());
        }
        log.info("66666");
        threadPoolExecutor.shutdown();
//        log.info(thread.getName() + "---ShutDown");

        return wb;
    }

    public static CellStyle getTitleStyle(XSSFWorkbook wb) {
        /**
         * 标题样式 样式
         */
        XSSFFont titleFont = wb.createFont();
        titleFont.setFontHeight(24);
        titleFont.setBold(true);
        CellStyle titleCellStyle = wb.createCellStyle();
        titleCellStyle.setBorderTop(BorderStyle.THIN);
        titleCellStyle.setBorderBottom(BorderStyle.THIN);
        titleCellStyle.setBorderLeft(BorderStyle.THIN);
        titleCellStyle.setBorderRight(BorderStyle.THIN);
        titleCellStyle.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        titleCellStyle.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        titleCellStyle.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        titleCellStyle.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        titleCellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
        titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 上下居中
        titleCellStyle.setFont(titleFont);

        return titleCellStyle;
    }

    public static CellStyle getCellStyle(XSSFWorkbook wb) {
        /**
         * 单元格 样式
         */
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        cellStyle.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        cellStyle.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        cellStyle.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        cellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 上下居中

        return cellStyle;
    }

}
